package Algorithm.LinkedListProblem;

/**
 * @Author cj
 * @Date 2021/11/18 20:01
 */
public class removeLastKthNode {

    class Node {
        public int value;
        public Node next;

        public Node(int val) {
            this.value = val;
        }
    }

    public static Node removeLastKthNode1(Node head, int lastKth) {
        if (head == null || lastKth < 1) {
            return head;
        }
        Node node = head;
        while (node != null) {
            lastKth --;
            node = node.next;
        }
        // K-N = 0
        if (lastKth == 0) {
            head = head.next;
        } else if (lastKth < 0){ // K-N < 0
            node = head;
            while (lastKth < 0) {
                node = node.next;
                lastKth ++;
            }
            // 删除
            node.next = node.next.next;
        }
        return head;
    }

}
